var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var Pagination = require('../../utils/pagination.js');

Page({
	data: {
		dataLoaded: false,
		nextPageLoading: false,
		pageLoadedAll: false,
		shopInfo: {},
		mineList: [],
		userList: [],
	},

	onLoad(options) {
		var _self = this;

		_self.shopId = options.shopId;

		_self._getPageData();
	},

	onReachBottom() {
		var _self = this;

		if (_self.data.pageLoadedAll) {
			return;
		}
		_self.pagination && _self.pagination.loadNext();
	},

	_back() {
		wx.navigateBack();
	},

	_toPay() {
		wx.navigateTo({
			url: '/pages/pay/pay?shopId=' + this.shopId
		});
	},

	_getPageData() {
		var _self = this;

		_self.pagination = new Pagination({
			url: '/openapi/User/getQueuePeople',
			params: {
				shopId: _self.shopId
			},
			beforeGetPage(page) {
				if (page > 1) {
					_self.setData({
						nextPageLoading: true
					});
				}
			},
			getPageSuccess(page, d) {
				var userList = _self.data.userList || [];
				var result = d.result;
				var curUserList = result['userList'] || [];
				var data = {};

				if (page == 1) {
					data = {
						dataLoaded: true,
						shopInfo: result['shopInfo'],
						mineList: result['myList'],
					};
				}

				if (curUserList.length == 0) {
					data.pageLoadedAll = true;
				}

				data.nextPageLoading = false;
				data.userList = userList.concat(curUserList);

				_self.setData(data);
			}
		});
	},
	_jump(evt){
		var url = evt.currentTarget.dataset.url + '?shopId=' + this.shopId;
		wx.navigateTo({
			url
		})
	}
});